package com.woniuxy.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.crm.entity.Customer;
import com.woniuxy.crm.service.ICustomerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户表 前端控制器
 *
 * @author 郑鹏
 * @since 2024-08-12
 */
@RestController
@RequestMapping("/admin/customer")
@Tag(name = "客户表接口")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    // 新增或者更新
    @PostMapping("/add")
    @Operation(summary = "新增或者修改")
    public boolean save(@RequestBody Customer customer) {
        return customerService.saveOrUpdate(customer);
    }

    @DeleteMapping("deleteById")
    @Operation(summary = "根据id删除")
    public Boolean delete(@RequestParam Long id) {
        return customerService.removeById(id);
    }

//    @PostMapping("/del/batch")
//    @Operation(summary = "批量删除")
//    public boolean deleteBatch(@RequestBody List<Integer> ids) {
//        return customerService.removeByIds(ids);
//    }

    @GetMapping("list")
    @Operation(summary = "查询全部")
    public List<Customer> findAll() {
        return customerService.list();
    }

    @GetMapping("/getById")
    @Operation(summary = "根据id查询")
    public Customer findOne(@RequestParam Long id) {
        return customerService.getById(id);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public Page<Customer> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return customerService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

}

